// ProgressBar.cpp: implementation of the ProgressBar class.
//
//////////////////////////////////////////////////////////////////////

#include "ProgressBar.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

ProgressBar::ProgressBar(float start_x, float start_y, float dim_x, float dim_y, int blocks, int colour)
{
this->blocks = blocks;
this->colour = colour;
this->progress = 0.0f;
this->start_x = start_x;
this->start_y = start_y;
this->dim_x = dim_x;
this->dim_y = dim_y;
}

ProgressBar::~ProgressBar()
{

}

void ProgressBar::display(float p)
{
// bounds
if(p < 0.0f) progress = 0.0f;
else if(p >= 1.0f) progress = 0.999999f; 
else progress = p;

// how many blocks should be drawn?
float blockDim = 0.95f / (float)blocks;
float freeDim;
if(blocks > 1) freeDim  = 0.05f / (float)(blocks-1);
else           freeDim  = 0.0f;

int fullBlocks = (int)( (float)blocks * progress );
if(fullBlocks > blocks) fullBlocks = blocks;
float semiBlockDim = progress - ( (float)fullBlocks * blockDim); 
if(semiBlockDim > blockDim) 
     {
     fullBlocks++;
     semiBlockDim = semiBlockDim - blockDim;
     }

//printf("fullBlocks: %d %d %f %f %f\n",blocks,fullBlocks,semiBlockDim,blockDim,freeDim);

blockDim = blockDim * dim_x;
freeDim  = freeDim  * dim_x;
semiBlockDim = semiBlockDim * dim_x;

/*Set up the 2D ambient */
int fh = (float)glutGet ( (GLenum) GLUT_WINDOW_HEIGHT ) ;
int fw = (float)glutGet ( (GLenum) GLUT_WINDOW_WIDTH  ) ;

  glDisable      ( GL_LIGHTING ) ;
  glDisable      ( GL_DEPTH_TEST ) ;
  glDisable      ( GL_CULL_FACE  ) ;

  glEnable       ( GL_ALPHA_TEST ) ;
  glEnable       ( GL_BLEND ) ;
  glAlphaFunc    ( GL_GREATER, 0.1f ) ;
  glBlendFunc    ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
  
  //create an projection that acts like a 2D screen
  glMatrixMode   ( GL_PROJECTION ) ;
  glPushMatrix   () ;
  glLoadIdentity () ;
  glOrtho        ( 0.0, 1.0, 0.0, fw/fh, 0.0, 1.0 ) ;

  glMatrixMode   ( GL_MODELVIEW ) ;
  glPushMatrix   () ;
  glLoadIdentity () ;   

    if(colour == PB_COLOR_SUNBURST) glColor4f(1.0f,0.70f,0.0f,1.0f);
    else if(colour == PB_COLOR_ICE) glColor4f(0.0f,0.40f,0.7f,1.0f);
    else if(colour == PB_COLOR_EMERALD) glColor4f(0.0f,0.7f,0.30f,1.0f);
  // draw bounds
    glBegin(GL_LINE_STRIP);
    glVertex2f(start_x - 0.01f, start_y - 0.01f);
    glVertex2f(start_x - 0.01f, start_y + dim_y + 0.01f);
    glVertex2f(start_x + dim_x + 0.01f, start_y + dim_y + 0.01f);
    glVertex2f(start_x + dim_x + 0.01f, start_y - 0.01f);
    glVertex2f(start_x - 0.01f, start_y - 0.01f);
    glEnd();
  
  // draw full blocks
  for(int i = 0; i < fullBlocks; i++)
    {
    glBegin(GL_QUADS);
    glVertex2f(start_x + ((float)i)*(blockDim+freeDim), start_y);
    glVertex2f(start_x + ((float)i)*(blockDim+freeDim), start_y + dim_y);
    glVertex2f(start_x + ((float)i)*(blockDim+freeDim) + blockDim, start_y + dim_y);
    glVertex2f(start_x + ((float)i)*(blockDim+freeDim) + blockDim, start_y);
    glEnd();
    }
  
  // draw semi blocks if any
    if(fullBlocks < blocks)
    {
    glBegin(GL_QUADS);
    glVertex2f(start_x + fullBlocks*(blockDim+freeDim), start_y);
    glVertex2f(start_x + fullBlocks*(blockDim+freeDim), start_y + dim_y);
    glVertex2f(start_x + fullBlocks*(blockDim+freeDim)+ semiBlockDim, start_y + dim_y);
    glVertex2f(start_x + fullBlocks*(blockDim+freeDim)+ semiBlockDim, start_y);
    glEnd();  
    }
  
 /* Back to 3d view */
	
  glMatrixMode   ( GL_PROJECTION ) ;
  glPopMatrix    () ;
  glMatrixMode   ( GL_MODELVIEW ) ;
  glPopMatrix    () ;
  
  glDisable      ( GL_ALPHA_TEST ) ;
  glDisable      ( GL_BLEND ) ;
  glAlphaFunc    ( GL_ALWAYS, 0.0 ) ;
  glBlendFunc    ( GL_ONE, GL_ZERO ) ;

  glEnable     ( GL_LIGHTING ) ;
  glEnable     ( GL_DEPTH_TEST ) ;
  glEnable      ( GL_CULL_FACE  ) ;
}
